package cn.yi.fast.security.shiro;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Collection;
import java.util.Set;

public class RedisCache<K,V> implements Cache<K,V> {

    private RedisTemplate redisTemplate;

    private String name;

    private Long timeout;

    public RedisCache(RedisTemplate redisTemplate, String name, Long timeout) {
        this.redisTemplate = redisTemplate;
        this.name = name;
        this.timeout = timeout;
    }

    @Override
    public V get(K k) throws CacheException {
        return (V) redisTemplate.opsForValue().get(name+k);
    }

    @Override
    public V put(K k, V v) throws CacheException {
        redisTemplate.opsForValue().set(name+k,v,timeout);
        return v;
    }

    @Override
    public V remove(K k) throws CacheException {
        V v = get(k);
        redisTemplate.delete(name+k);
        return v;
    }

    @Override
    public void clear() throws CacheException {

    }

    @Override
    public int size() {
        return 0;
    }

    @Override
    public Set<K> keys() {
        return Sets.newHashSet();
    }

    @Override
    public Collection<V> values() {
        return Lists.newArrayList();
    }
}
